package com.example.Nurse_Notifier;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import interfaces.ChangeState;

import java.math.BigInteger;

public class ListFragment extends Fragment implements ChangeState {

    StringItemAdapter itemAdapter;
    FragmentActivity activity;
    XbeeDeviceContainer xbeeDeviceContainer;
    private int currentPos = 0;
    DeviceActivity devActivity;

    String[] statuses = {
            "выключено",
            "простаивает",
            "сканирует",
            "оповещает"
    };
    Intent intent;

    public ListFragment(XbeeDeviceContainer xbeeDeviceContainer, FragmentActivity activity) {
        this.activity = activity;
        this.xbeeDeviceContainer = xbeeDeviceContainer;
        itemAdapter = new StringItemAdapter(activity.getApplicationContext(), R.layout.list_row,
                this.xbeeDeviceContainer);
    }

    public StringItemAdapter getItemAdapter() {
        return itemAdapter;
    }

    public int getCurrentPos() {
        return currentPos;
    }

    public void setCurrentPos(int currentPos) {
        this.currentPos = currentPos;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.listlayout, container, false);
        ListView devsList = (ListView) rootView.findViewById(R.id.devicesListView);
        devsList.setAdapter(itemAdapter);

        devsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setBackgroundColor(0xffffff);
                setCurrentPos(position);
                devActivity = new DeviceActivity();
                intent = new Intent(activity.getApplicationContext(), devActivity.getClass());
                intent.putExtra("devNum", "" + (xbeeDeviceContainer.get(position).getId()));
                intent.putExtra("devMac", new BigInteger(1, xbeeDeviceContainer.get(position).getMac()).toString(16));
                intent.putExtra("devStatus", statuses[xbeeDeviceContainer.get(position).getStatus()]);
                intent.putExtra("intStatus", xbeeDeviceContainer.get(position).getStatus());
                startActivity(intent);
            }
        });
        return rootView;
    }

    @Override
    public void addObject(final XbeeDevice object) {
        xbeeDeviceContainer.add(object);
        Log.i("SK", "Add object to adapter");
        itemAdapter.notifyDataSetChanged();
        Log.i("SK", "Notifying about adding");
    }

    @Override
    public void removeObject(final int indx) {
        xbeeDeviceContainer.remove(indx);
        itemAdapter.notifyDataSetChanged();
    }

    @Override
    public void changeObjectStatus(final int objectId, final int status) {
        xbeeDeviceContainer.get(objectId).changeDeviceStatus(status);
        itemAdapter.notifyDataSetChanged();
    }
}